<?php

// Add a menu for bcer Options to the Settings drop down
add_action('admin_menu', 'bc_add_page');

function bc_add_page() {
	add_options_page( 'Book Compare Options', 'Book Compare Options', 'manage_options', 
		'bc_options_page', 'bc_options_page_display');
}

// bcer Options Page Display Function
function bc_options_page_display() { ?>
	<div class="wrap">
		<?php screen_icon(); ?>
		<h2>Book Compare Options</h2>
		<form action="options.php" method="post">
				<?php settings_fields('bc_options'); ?>
				<?php do_settings_sections('bc_options_page'); ?>
				<input name="Submit" type="submit" value="Save Changes" />
		</form>
	</div>
	<?php
}

// Register and define the settings
add_action('admin_init', 'bc_admin_init');

function bc_admin_init() {
	register_setting( 
		'bc_options', 
		'bc_options',
		'bc_validate_options' 
		);
	add_settings_section( 
		'bc_plugin_main', 
		'Default Configuration', 
		'bc_section_text', 
		'bc_options_page'); 
	add_settings_field(
		'bc_options_title',
		'Amazon Options (Required)',
		'bc_amazon_setting_input',
		'bc_options_page',
		'bc_plugin_main'
		);
	add_settings_field(
		'bc_half_setting_input',
		'Half/eBay.com Options',
		'bc_half_setting_input',
		'bc_options_page',
		'bc_plugin_main'
		);
	add_settings_field(
		'bc_cj_setting_input',
		'Commission Junction Options',
		'bc_cj_setting_input',
		'bc_options_page',
		'bc_plugin_main'
		);
}

// Admin Init Call Back Functions

//Explantion about this section
function bc_section_text() {
	//echo '<p>Choose your configuration style here.</p>';
}

// Display and fill the form field
function bc_amazon_setting_input() {
	$options = get_option( 'bc_options' );
	
	if ($options == false) {
		$options = array();
	}
	
	$defaults = array(
		'public_key' => 'AKIAJ4F4UMPIPCASIVVA',
		'private_key' => '0PK5fK91f43uaP5xObkCj/T0iotLdADPq7mjwMrm',
		'region' => 'com',
		'associate_tag' => 'campuspost-20',
		'default_keyword' => '0385486804',
		'price_display' => 'LowestUsedPrice',
		'search_index' => 'Books'
	);
	
	$options = wp_parse_args($options, $defaults);
	extract($options);
	

	?>
		<div class="config_options_table">
			Public Key: <input id='plubic_key' name='bc_options[public_key]' 
				type='text' value='<?php echo $public_key ?>' /><br />
			Private Key: <input id='private_key' name='bc_options[private_key]' 
				type='text' value='<?php echo $private_key ?>' /><br />
			Associate Tag: <input id='associate_tag' name='bc_options[associate_tag]' 
				type='text' value='<?php echo $associate_tag ?>' /><br />
			Region: <input id='region' name='bc_options[region]' 
				type='text' value='<?php echo $region ?>' /><br />
			Default Keyword: <input id='default_keyword' name='bc_options[default_keyword]' 
				type='text' value='<?php echo $default_keyword ?>' /><br />
			Default Search Index: <input id='search_index' name='bc_options[search_index]' 
				type='text' value='<?php echo $search_index ?>' /><br />
			<p>Price Display: 
				<select name="bc_options[price_display]" >
					<option value="LowestUsedPrice" <?php if ( $price_display == 'LowestUsedPrice' ) echo 'selected="selected"'; ?>>Lowest Used Price</option>
					<option value="LowestNewPrice" <?php if ( $price_display == 'LowestNewPrice' ) echo 'selected="selected"'; ?>>Lowest New Price</option>
					<option value="LowestCollectablePrice" <?php if ( $price_display == 'LowestCollectablePrice' ) echo 'selected="selected"'; ?>>Lowest Collectable Price</option>
				</select><br />
			<strong>Note:</strong> If one or more of the prices are not available the default is lowest used.
	
		</div>
				
				
		
	<?php
}

// Display and fill the form field
function bc_half_setting_input() {
	$options = get_option( 'bc_options' );
	
	if ($options == false) {
		$options = array();
	}
	
	$defaults = array(
		'security_app_name' => 'JTWVentu-5405-49df-a2af-21c75dbaaa0f',
		'affiliate_networkid' => '',
		'affiliate_trackingid' => '',
		'affiliate_customid' => ''
	);
	
	$options = wp_parse_args($options, $defaults);
	extract($options);
	

	?>
		<div class="config_options_table">
			Security App Name: <input id='plubic_key' name='bc_options[security_app_name]' 
				type='text' value='<?php echo $security_app_name ?>' /><br />
			Affiliate Network Id: <input id='private_key' name='bc_options[affiliate_networkid]' 
				type='text' value='<?php echo $affiliate_networkid ?>' /><br />
			Affiliate Tracking Id: <input id='associate_tag' name='bc_options[affiliate_trackingid]' 
				type='text' value='<?php echo $affiliate_trackingid ?>' /><br />
			Affiliate Custom Id: <input id='associate_tag' name='bc_options[affiliate_customid]' 
				type='text' value='<?php echo $affiliate_customid ?>' /><br />
		</div>
				
	<?php
}

// Display and fill the form field
function bc_cj_setting_input() {
	$options = get_option( 'bc_options' );
	
	if ($options == false) {
		$options = array();
	}
	
	$defaults = array(
		'website_id' => '5724635',
		'cj_id' => '00aecea7385f3077c419a9e008e7e7d7360e3b1ffc64dffe4a78322ae1e60fab31a4d55329ac4343e7a4ae702de97978fbdaedf743afcbcd549fa8964a2a38afb7/04bf702343d2992e26544ad5731704ed5eed942cd2c092b38b1831b9f8f9abf89db215b935ee35ac2da638400aa03d5e81c220c2432a333c2d29fcce101bc201',
		'advertiser_ids' => ''
	);
	
	$options = wp_parse_args($options, $defaults);
	extract($options);
	

	?>
		<div class="config_options_table">
			Website Id: <input id='website_id' name='bc_options[website_id]' 
				type='text' value='<?php echo $website_id ?>' /><br />
			Commission Junction ID: <input id='cj_id' name='bc_options[cj_id]' 
				type='text' value='<?php echo $cj_id ?>' /><br />
			Advertiser ID: <input id='advertiser_ids' name='bc_options[advertiser_ids]' 
				type='text' value='<?php echo $advertiser_ids ?>' /><br />
				<p>Use 'joined' if you would only like to include services you have a relationship with.</p>
		</div>
				
	<?php
}

// Validate user input (we want text only)
function bc_validate_options( $input ) {
	if ($input['price_display'] === 'LowestUsedPrice' or 
		$input['price_display'] === 'LowestNewPrice' or 
		$input['price_display'] === 'LowestCollectablePrice') {
			return $input;
		}
	else {
		$input['price_display'] = 'LowestUsedPrice';
		return $input;
	}	
}
?>